package com.example.dishit.view.main;

import java.util.ArrayList;
import java.util.List;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dishit.R;
import com.example.dishit.controller.main.MainController;
import com.example.dishit.model.dbproxy.DBProxyFactory;
import com.example.dishit.model.dbproxy.IDBProxyCallback;
import com.example.dishit.model.dish.DishData;

public class SentDishesFragment extends ListFragment implements IDishesFragment {
    private DishArrayAdapter adapter;
    
    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        adapter = new DishArrayAdapter(container.getContext(), new ArrayList<DishData>());
        setListAdapter(adapter);
        final View view = inflater.inflate(R.layout.fragment_sent_dishes, container, false);
        
        // Get all DishDatas ever sent.
        final IDBProxyCallback callback = new IDBProxyCallback() {
            public void onResultCallback(boolean wasSuccessful, String message) {}
            public void onDishersCallback(List<String> myDishers) {}
            public void onDishDataCallback(final List<DishData> dishDatas) {
                MainController.instance().setSentDishes(dishDatas);
            }
        };
        // TODO: Eventually need a method to get last 'x' number of Dishes.
        DBProxyFactory.instance().getLatestSentDishes(MainController.THIS_USER_ID, -1, callback);
        return view;
    }
    
    public void addDish(DishData dish) {
        adapter.insert(dish, 0);
    }
    
    public void setDishes(List<DishData> dishes) {
        adapter.clear();
        adapter.addAll(dishes);
    }
}
